﻿using System;
using System.Windows.Forms;
using RainEngine.Rendering;
using RainEngine.SceneManagement;
using RainEngine.SceneNodes;

namespace SceneEditor
{
    public partial class MainForm : Form,IEditorHost 
    {
        // 编辑器主类，从引擎类继承
        private SceneEditor editor;

        // 当前使用的场景
        Scene scene;

        private TreeNode tnRoot;

        private bool isInitialized = false;
        
        public MainForm()
        {
            InitializeComponent();
        }

        #region 实现IWinForm接口和IEditorHost接口的成员
        
        public Form Form
        {
            get { return this; }
        }
        
        public Control winControl
        {
            get { return panel1 ; }
        }               
        
        public SceneEditor Editor
        {
            set { editor = value; }
        }        

        #endregion

        private void MainForm_Activated(object sender, EventArgs e)
        {
            Application.DoEvents();

            
                this.scene = editor.viewScene;
                isInitialized = true;
                // 添加事件
                editor.ItemSelected += new ItemSelected(editor_ItemSelected);
                panel1.Select();
                // 填充目录树
                TvwFillAll();
            
        }
        
        /// <summary>
        /// 当点击panel时就要判断是否选取了场景中的一个对象
        /// </summary>
        private void panel1_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            
            //------------------------------------------
            //  检查是否按下了鼠标左键
            //------------------------------------------
            MouseEventArgs me = (MouseEventArgs)e;
            if ((me.Button & MouseButtons.Left) == me.Button)
            {
                // 进行拾取检测
                editor.PickViewPort(me.X, me.Y);
            }
            panel1.Select();
            Cursor = Cursors.Default;
        }

        /// <summary>
        /// 当节点被选择，则更新属性窗口
        /// </summary>
        /// <param name="node"></param>
        void editor_ItemSelected(object node)
        {
            propertyGrid1.SelectedObject = node;
        }

        #region TreeView处理程序

        private void tvwScene_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = tvwScene.SelectedNode;
            // 如果选择的是根节点，则显示场景的属性
            if (node == tvwScene.Nodes[0])
                editor.SetSelection(scene);
            else
            {
                if (node == null)
                    editor.SetSelection(null);
                else
                    editor.SetSelection(node.Tag);
            }
            panel1.Select();
        }

        
        /// <summary>
        /// 单击Tree节点获取选择的节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwScene_Click(object sender, EventArgs e)
        {
            MouseEventArgs me = (MouseEventArgs)e;
            if (me.Button == MouseButtons.Left)
            {
                TreeNode tn = tvwScene.GetNodeAt(me.Location);
                if (tn == null)
                    editor.SetSelection(null);
                else
                    editor.SetSelection(tn.Tag);
            }
        }
        
        /// <summary>
        /// 获取目录树中要显示的节点文字
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private string TvwGetNodeText(SceneNode node)
        {
            string desc;
            Type t = node.GetType();
            switch (t.Name)
            {
                case "PlaneSceneNode":
                    desc = "平面：";
                    break;
                case "BillboardSceneNode":
                    desc = "Billboard：";
                    break;
                case "SphereSceneNode":
                    desc = "球：";
                    break;
                case "CubeSceneNode":
                    desc = "立方体：";
                    break;
                case "ModelSceneNode":
                    desc = "模型：";
                    break;
                case "SkyDomeSceneNode":
                    desc = "天空球：";
                    break;
                case "SkyBoxSceneNode":
                    desc = "天空盒：";
                    break;
                case "SimpleTerrainSceneNode":
                    desc = "简单地形：";
                    break;
                default:
                    desc = "Ud:";
                    break;
            }
            return desc + " " + node.Name;
        }

        /// <summary>
        /// 根据场景中的节点填充目录树
        /// </summary>
        private void TvwFillAll()
        {
            TreeNode node = null;
            tvwScene.Nodes.Clear();
            //if (scene == null)
             //   return;

            //------------------------------------
            //  根节点为场景名称
            //------------------------------------
            tnRoot = tvwScene.Nodes.Add(scene.Name);
            tnRoot.Name = "Scene";

            //------------------------------------
            //  添加节点add Nodes (holds all SceneNodes)
            //------------------------------------
            TreeNode sceneNodesRoot = new TreeNode("Nodes");
            sceneNodesRoot.Name = "Nodes";
            tnRoot.Nodes.Add(sceneNodesRoot);

            //------------------------------------
            //  添加所有场景节点 
            //------------------------------------
            for (int i = 0; i<scene.NodesCount; i++)
            {
                node = new TreeNode(TvwGetNodeText(scene.Nodes [i]));
                node.Tag = scene.Nodes[i];
                sceneNodesRoot.Nodes.Add(node);
            }
            if (node != null)
                tvwScene.SelectedNode = node;

            //------------------------------------
            //  添加光源节点
            //------------------------------------
            TreeNode lightsRoot = new TreeNode("Lights");
            lightsRoot.Name = "Lights";
            tnRoot.Nodes.Add(lightsRoot);
            for (int i = 0; i < scene.lights .Length ; i++)
            {
                Light l = scene.lights [i];
                string s = l.LightType.ToString();
                node = new TreeNode(s);
                node.Tag = l;
                lightsRoot.Nodes.Add(node);
            }
            tvwScene.SelectedNode = node;


            //------------------------------------
            //  添加相机节点 
            //------------------------------------
            TreeNode cameraRoot = new TreeNode("Camera");
            cameraRoot.Name = "Camera";
            cameraRoot.Tag = scene.Camera;
            tnRoot.Nodes.Add(cameraRoot);

        }
        #endregion

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
